package com.kwan.xframe.mvvm.view.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;

import com.kwan.xframe.immersionbar.ImmersionBar;
import com.kwan.xframe.R;
import com.kwan.xframe.common.bean.TitleEntity;
import com.kwan.xframe.common.image.ImageLoader;
import com.kwan.xframe.common.image.glide.GlideLoader;
import com.kwan.xframe.databinding.LayoutBaseBinding;
import com.kwan.xframe.mvvm.binding.DataBindingConfig;
import com.kwan.xframe.mvvm.scope.ViewModelScope;
import com.kwan.xframe.mvvm.view.activity.BaseActivity;
import com.kwan.xframe.view_model.BaseViewModel;
import com.trello.rxlifecycle4.components.support.RxFragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModel;
import timber.log.Timber;


/**
 * fragment基类
 * Created by Mr.Kwan on 2016-5-4.
 */
public abstract class BaseFragment<V extends ViewDataBinding> extends RxFragment implements View.OnClickListener {

	public static boolean LOG = true;
	protected LayoutInflater mInflater;
	protected BaseActivity<?> mBaseActivity;

	protected Context mContext;
	/**
	 * fragment最外层包装view
	 */
	protected View mBaseContentView;
	protected View mContentView;
	protected ImageLoader mImageLoader;

	@Override
	public void onAttach(@NonNull Context context) {
		super.onAttach(context);
		mContext = context;
		mBaseActivity = (BaseActivity<?>) context;
		mInflater = mBaseActivity.getLayoutInflater();
		mImageLoader = new GlideLoader();
		if (LOG) {
			Timber.d("onAttach");
		}
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (LOG) {
			Timber.d("onCreate");
		}
	}

	@Nullable
	@Override
	public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		if (LOG) {
			Timber.d("onCreateView");
		}
		return initBaseContentView(inflater, container, savedInstanceState);
	}

	@Override
	public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		if (LOG) {
			Timber.d("onViewCreated");
		}
		if (isImmersionBarEnabled())
			initImmersionBar();

	}

	@Override
	public void onStart() {
		super.onStart();
		if (LOG) {
			Timber.d("onStart");
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if (LOG) {
			Timber.d("onResume");
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		if (LOG) {
			Timber.d("onPause");
		}
	}

	@Override
	public void onStop() {
		super.onStop();
		if (LOG) {
			Timber.d("onStop");
		}
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		if (LOG) {
			Timber.d("onDestroyView");
		}
	}

	@Override
	public void onDetach() {
		super.onDetach();
		if (LOG) {
			Timber.tag(getTag()).d("onDetach");
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {

	}

	LayoutBaseBinding mBaseBinding;
	protected V mBinding;

	protected abstract DataBindingConfig getDataBindingConfig();


	private View initBaseContentView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

		DataBindingConfig dataBindingConfig = getDataBindingConfig();
		mBaseBinding = DataBindingUtil.inflate(inflater, R.layout.layout_base, container, false);
		Timber.e("mBaseBinding:" + mBaseBinding);

		if (mBaseBinding != null) {
			mBaseBinding.setLifecycleOwner(this);
			mBaseBinding.setTitle(getTitleEntity());
			mBaseContentView = mBaseBinding.clRoot;

		} else {
			mBaseContentView = mInflater.inflate(R.layout.layout_base, container, false);
		}

		ViewStub stubMainContent = mBaseContentView.findViewById(R.id.view_stub_content);

		if (stubMainContent != null) {
			stubMainContent.setLayoutResource(dataBindingConfig.getLayout());
			mContentView = stubMainContent.inflate();
			Timber.e("mContentView:%s", mContentView);
		}

		if (mBaseContentView != null) {

			mBinding = DataBindingUtil.bind(mContentView);
			assert mBinding != null;
			mBinding.setLifecycleOwner(this);

			Timber.e("mBaseContentView != null:%s", mBinding);

			if (dataBindingConfig.getStateViewModel() != null) {
				getLifecycle().addObserver(dataBindingConfig.getStateViewModel());
				if (dataBindingConfig.getVmVariableId() != -1) {
					mBinding.setVariable(dataBindingConfig.getVmVariableId(), dataBindingConfig.getStateViewModel());
				}
			}

			SparseArray<BaseViewModel> bindingParams = dataBindingConfig.getBindingParams();
			if (bindingParams != null) {
				for (int i = 0, length = bindingParams.size(); i < length; i++) {
					if (bindingParams.valueAt(i) != null) {
						//让ViewModel拥有View的生命周期感应
						getLifecycle().addObserver(bindingParams.valueAt(i));
						if (bindingParams.keyAt(i) != -1)
							mBinding.setVariable(bindingParams.keyAt(i), bindingParams.valueAt(i));
					}
				}
			}
		} else {
			Timber.e("mBaseContentView == null:%s", mBinding);
		}

		return mBaseContentView;

	}


	private final ViewModelScope mViewModelScope = new ViewModelScope();

	protected <T extends ViewModel> T getFragmentScopeViewModel(@NonNull Class<T> modelClass) {
		return mViewModelScope.getFragmentScopeViewModel(this, modelClass);
	}

	protected <T extends ViewModel> T getActivityScopeViewModel(@NonNull Class<T> modelClass) {
		return mViewModelScope.getActivityScopeViewModel(mBaseActivity, modelClass);
	}

	protected <T extends ViewModel> T getApplicationScopeViewModel(@NonNull Class<T> modelClass) {
		return mViewModelScope.getApplicationScopeViewModel(modelClass);
	}

	protected abstract TitleEntity getTitleEntity();


	/**
	 * 是否对用户可见
	 */
	protected boolean mIsVisible;
	/**
	 * 是否加载完成
	 * 当执行完onViewCreated方法后即为true
	 */
	protected boolean mIsPrepare;

	/**
	 * 是否加载完成
	 * 当执行完onViewCreated方法后即为true
	 */
	protected boolean mIsImmersion;

	/**
	 * 是否懒加载
	 *
	 * @return the boolean
	 */
	protected boolean isLazyLoad() {
		return true;
	}

	/**
	 * 是否在Fragment使用沉浸式
	 *
	 * @return the boolean
	 */
	protected boolean isImmersionBarEnabled() {
		return false;
	}

	/**
	 * 用户可见时执行的操作
	 */
	protected void onVisible() {
		onLazyLoad();
	}

	private void onLazyLoad() {

		if (mIsVisible && mIsPrepare) {
			mIsPrepare = false;
		}

		if (mIsVisible && mIsImmersion && isImmersionBarEnabled()) {
			initImmersionBar();
		}
	}

	public ImmersionBar mImmersionBar;

	/**
	 * 初始化沉浸式
	 */
	protected void initImmersionBar() {
		mImmersionBar = ImmersionBar.with(this);
		mImmersionBar.transparentStatusBar().navigationBarWithKitkatEnable(false).init();
	}

}
